package com.ttloner.admin.controller;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author TTLoner
 * @date 2021/7/20 23:09
 * @description
 */
@Slf4j
@Controller
public class FormController {
    @GetMapping("/form_layouts")
    public String formLayouts(){
        return  "form/form_layouts";
    }
    //MultipartFile 自动封装上传过来的文件
    @PostMapping("/upload")
    public String upload(@RequestParam("email") String email, @RequestParam("name") String name,
                         @RequestPart("headerImg") MultipartFile headerImg,
                         @RequestPart("photos") MultipartFile[] photos) throws IOException {
        log.info("上传的信息：email={},name={},headerImg={},photos={}",
                email, name, headerImg.getSize(), photos.length);
        if (!headerImg.isEmpty()) {
            String imgName = headerImg.getOriginalFilename();
            headerImg.transferTo(new File("C:\\Users\\TTLoner\\Pictures\\" + imgName));
        }
        if (photos.length > 0) {
            for (MultipartFile photo : photos) {
                photo.transferTo(new File("C:\\Users\\TTLoner\\Pictures\\" + photo.getOriginalFilename()));
            }
        }
        return "main";
    }
}
